package com.wom.blockfeatures;

import com.wom.blocks.AbstractBlock;
import com.wom.blocks.AirBlock;
import com.wom.blocks.SandBlock;
import com.wom.blocks.TreeLeavesBlock;
import com.wom.blocks.WaterBlock;
import com.wom.gui.BlockTypes;
import com.wom.worldmaker.GameProperties;
import com.wom.worldmaker.GameWorld;

public abstract class BlockUtils {
	
	


	public static AbstractBlock getOnBlock(int x,int y){


		for(int i=0;i<GameWorld.gameBlocks.length;i++){
			for(int j=0;j<GameWorld.gameBlocks[0].length;j++){
				if(GameWorld.gameBlocks[i][j].contains(x/GameProperties.pixelRatio+GameWorld.player.cam.x,y/GameProperties.pixelRatio+GameWorld.player.cam.y)){
					return GameWorld.gameBlocks[i][j];
				}
			}
		}




		return null;

	}



	public static AbstractBlock getBlockBelow(AbstractBlock b){


		if(b.getyPos()<GameWorld.gameBlocks[0].length){
			return GameWorld.gameBlocks[b.getxPos()][b.getyPos()+1];
		}else
			return null;



	}

	public static AbstractBlock getBlockAbove(AbstractBlock b){


		if(b.getyPos()>0){
			return GameWorld.gameBlocks[b.getxPos()][b.getyPos()-1];
		}else
			return null;





	}


	public static AbstractBlock getBlockLeft(AbstractBlock b){


		if(b.getxPos()>1){
			return GameWorld.gameBlocks[b.getxPos()-1][b.getyPos()];
		}else
			return null;



	}


	public static AbstractBlock getBlockRight(AbstractBlock b){


		if(b.getxPos()<GameWorld.gameBlocks.length-1){
			return GameWorld.gameBlocks[b.getxPos()+1][b.getyPos()];
		}else
			return null;



	}


	public static boolean isContainedBlock(AbstractBlock b, BlockTypes t){

		boolean check=false;

		if(t==BlockTypes.air){
			if(getBlockAbove(b) instanceof AirBlock && !(getBlockBelow(b) instanceof AirBlock)
					&& !(getBlockLeft(b) instanceof AirBlock) && !(getBlockRight(b) instanceof AirBlock)){
				check=true;

			}else check=false;


		}
		return check;
	}
	
	
	public static boolean isLeftRightBlocked(AbstractBlock b, BlockTypes t){

		boolean check=false;

		if(t==BlockTypes.water){
			if(getBlockBelow(b) instanceof AirBlock 
					&& !(getBlockLeft(b) instanceof AirBlock) && (getBlockRight(b) instanceof AirBlock)){
				check=true;

			}else check=false;


		}
		return check;
	}


	public static boolean isLeftCorneredBlock(AbstractBlock b, BlockTypes t){

		boolean check=false;

		if(t==BlockTypes.air){
			if(getBlockAbove(b) instanceof AirBlock &&
					!(getBlockBelow(b) instanceof AirBlock)	&&
					!(getBlockLeft(b) instanceof AirBlock) &&
					(getBlockRight(b) instanceof AirBlock)){
				check=true;

			}else check=false;


		}
		return check;
	}

	public static boolean isRightCorneredBlock(AbstractBlock b, BlockTypes t){

		boolean check=false;

		if(t==BlockTypes.air){
			if(getBlockAbove(b) instanceof AirBlock && 
					!(getBlockBelow(b) instanceof AirBlock)	&&
					(getBlockLeft(b) instanceof AirBlock) && 
					!(getBlockRight(b) instanceof AirBlock)){
				check=true;

			}else check=false;


		}
		return check;
	}

	public static boolean isIsolatedBlock(AbstractBlock b, BlockTypes t){

		boolean check=false;

		if(t==BlockTypes.air){
			if(getBlockAbove(b) instanceof AirBlock && getBlockBelow(b) instanceof AirBlock
					&& getBlockLeft(b) instanceof AirBlock && getBlockRight(b) instanceof AirBlock){
				check=true;

			}else check=false;



			return check;
		}

		if(t==BlockTypes.Sand){
			if(getBlockAbove(b) instanceof SandBlock && getBlockBelow(b) instanceof SandBlock
					&& getBlockLeft(b) instanceof SandBlock && getBlockRight(b) instanceof SandBlock){
				check=true;

			}else check=false;



			return check;
		}

		return false;

	}

	public static void changeInto(int x, int y,BlockTypes t){

		
		if(t==BlockTypes.Sand){
			GameWorld.gameBlocks[x][y]=new	 SandBlock(x, y);
		}else

			if(t==BlockTypes.water){
				GameWorld.gameBlocks[x][y]=new	 WaterBlock(x, y);
			}else

				if(t==BlockTypes.air){
					GameWorld.gameBlocks[x][y]=new	 AirBlock(x, y);
				}else

					if(t==BlockTypes.leafBlock){
						GameWorld.gameBlocks[x][y]=new	 TreeLeavesBlock(x, y);
					}






	}
	



public static boolean isIsolatedBothSidesBlock(WaterBlock b,BlockTypes t) {
	boolean check=false;

	if(t==BlockTypes.air){
		if(getBlockAbove(b) instanceof AirBlock && !(getBlockBelow(b) instanceof AirBlock)
				&& getBlockLeft(b) instanceof AirBlock && getBlockRight(b) instanceof AirBlock){
			check=true;

		}else check=false;



		return check;
	}
	return check;
}








}
